using System;
using System.Collections;
using System.Collections.Specialized;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using Sedna.Core.Controller;
using Sedna.Core.Controller.Configurations;
using Sedna.Core.Controls.Support;
using Sedna.Core.Controls.Support.Javascript;
using Sedna.Core.Tiles.Contents;

namespace Sedna.Core.Controls
{
	/// <summary>
	/// Radio button to be used as DataGrid column. There is a problem with standart radio button
	/// (either html & web control) - it uses NamingContainer UniqueID as prefix for its name attribute,
	/// so radio button from different rows in DataGrid cannot be in the same radio group. This control 
	/// lacks this problem.
	/// </summary>
	public class DataGridRadioButton: HtmlInputControl, IContent, IPostBackDataHandler, IConfigurationAware, IClientScriptingControl, ICommandAware
	{
		protected IConfiguration configuration;
		protected string configurationId;

		private bool clientScriptingEnabled=false;
		private string clientVariable;

		protected string name;

        private string javascriptClass;
	    
		private String contentPlaceHolderId;

        #region Properties

        public String ContentPlaceHolderId
		{
			get { return contentPlaceHolderId;}
			set { contentPlaceHolderId = value;}
		}

		public string CommandArgument
		{
			get { return null; }
			set {  }
		}

		public string CommandName
		{
			get { return null; }
			set {  }
		}

		public string OnClick
		{
			get { return Attributes["onclick"]; }
			set { Attributes["onclick"]=value; }
		}

		public override string Name
		{
			get { return name; }
			set { name = value; }
		}

		public virtual string GroupName
		{
			get { return Name; }
			set { Name = value; }
		}

		public IConfiguration Configuration
		{
			get { return configuration; }
			set { configuration = value; }
		}

		public string ConfigurationId
		{
			get { return configurationId; }
			set { configurationId = value; }
		}

		public bool Checked
		{
			get 
			{
				string isChecked = base.Attributes["checked"];
				if (isChecked == null)
				{
					return false;
				}
				return isChecked.Equals("checked");
			}
			set
			{
				if (value)
				{
					base.Attributes["checked"] = "checked";
				}
				else
				{
					base.Attributes["checked"] = null;
				}
			}
		}

        public bool ClientScriptingEnabled
        {
            get { return clientScriptingEnabled /*|| clientVariable != null*/; }
            set { clientScriptingEnabled = value; }
        }

        public string ClientVariable
        {
            get { return ControlUtil.GetClientVariable(this, clientVariable); }
            set { clientVariable = value; }
        }

        #endregion

        #region Javascript support

        public string JavascriptClass
        {
            get { return javascriptClass; }
            set { javascriptClass = value; }
        }

        public string NodeName
        {
            get { return ClientID; }
        }


        #endregion



        public DataGridRadioButton(IConfiguration configuration) : base("radio")
		{
			Attributes["class"]="radio";
			this.configuration = configuration;
			this.configurationId = configuration.ObjectName;
			BuildControlFromConfiguration();
		}

		public bool LoadPostData(string postDataKey, NameValueCollection postCollection)
		{
			Value=postCollection[postDataKey];
			return false;
		}

		public void RaisePostDataChangedEvent()
		{
		}

		private void BuildControlFromConfiguration()
		{
			this.ID = configuration.Id;
			IRadioButtonConfiguration radioButtonConfiguration=Configuration as IRadioButtonConfiguration;
			if (radioButtonConfiguration==null)
			{
				return;	
			}
			ClientScriptingEnabled=radioButtonConfiguration.ClientScriptingEnabled;
			ClientVariable=radioButtonConfiguration.ClientVariable;
			GroupName=radioButtonConfiguration.GroupName;	
			Checked=radioButtonConfiguration.Checked;

			if (configuration.ClientAttributes!=null)
			{
				foreach (DictionaryEntry entry in configuration.ClientAttributes)
				{
					Attributes[(string)entry.Key]=(string)entry.Value;
				}			
			}

		}

		protected override void OnPreRender(EventArgs e)
		{
			base.OnPreRender(e);
			if (ClientScriptingEnabled)
			{
				EnableClientScripting();
			}
		}

		private void EnableClientScripting()
		{
            JavascriptController controller = JavascriptController.Current;

			controller.DeclareClass("RadioGroup");
            controller.DeclareObjectWithQuotedParams(ClientVariable, "RadioGroup", GroupName);
		}
	}

	/// <summary>
	/// Simple WebControl wrapper class used to be able to bind dataTranslator to column configuration 
	/// </summary>
/*
	public class ImsDataGridRadioButtonWrapper: WebControl
	{
		private DataGridRadioButton innerButton; 

		public string Value
		{
			get { return innerButton.Value; }
			set { innerButton.Value = value; }
		}
		public ImsDataGridRadioButtonWrapper(IConfiguration configuration): base()
		{
			innerButton=new DataGridRadioButton(configuration);
			Controls.Add(innerButton);
		}
	}
*/

}
